    package com.aries.crossgate.modules.sys.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aries.crossgate.common.utils.JsonDataFormate;
import com.aries.crossgate.common.utils.R;
import com.aries.crossgate.modules.sys.entity.SysMenu;
import com.aries.crossgate.modules.sys.service.SysMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 系统用户管理
 * @ProjectName: crossgate
 * @Package: com.aries.crossgate.modules.sys.controller
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 李贝留斯
 * @CreateDate: 2018/5/2 15:52
 * @Version: 1.0
 **/
@Controller
@RequestMapping(value="sys/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;
    /**
     * @description: 用户列表
     * @author 李贝留斯
     * @email sunyadongsyd@163.com
     * @date 2018/5/12 21:04
     */
    @RequestMapping(value = "/list.html")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("base/menu/menuList");
        return modelAndView;
    }

    @RequestMapping(value = "/add.html")
    public ModelAndView add(String parentId,String parentName){
        ModelAndView modelAndView = new ModelAndView();
        List<SysMenu> list = sysMenuService.getOneTwoMenu();
        modelAndView.setViewName("base/menu/menuAdd");
        modelAndView.addObject("menuList",list);
        modelAndView.addObject("parentId",parentId);
        modelAndView.addObject("parentName",parentName);
        return modelAndView;
    }

    @RequestMapping(value = "/edit.html")
    public ModelAndView edit(String id){
        ModelAndView modelAndView = new ModelAndView();
        SysMenu sysMenu = sysMenuService.getMenuById(id);
        modelAndView.setViewName("base/menu/menuEdit");
        modelAndView.addObject("SysMenu",sysMenu);
        return modelAndView;
    }

    @RequestMapping(value = "/save.html")
    @ResponseBody
    public R save(SysMenu sysMenu){
        R r = sysMenuService.save(sysMenu);
        return r;
    }

    @RequestMapping(value = "/delete.html")
    @ResponseBody
    public R delete(String ids){
        R r = sysMenuService.delete(ids);
        return r;
    }

    @RequestMapping(value = "/update.html")
    @ResponseBody
    public R update(SysMenu sysMenu){
        R r = sysMenuService.update(sysMenu);
        return r;
    }

    @RequestMapping(value = "/getSysMenuTreeData.html")
    @ResponseBody
    public String getSysMenuTreeData(){
        List<SysMenu> orgList = sysMenuService.getSysMenu();
        String jsonText = JSON.toJSONString(orgList, true);
        return jsonText;
    }

    @RequestMapping(value = "/getSysMenuData.html")
    @ResponseBody
    public JSONObject getSysMenuData(int page, int limit,String name,String menuId){
    //public JSONObject getOrgData(@RequestBody Map map){
        PageHelper.startPage(page,limit);
        List<SysMenu> userList = sysMenuService.getSysMenu(name, menuId);
        PageInfo pageInfo = new PageInfo(userList);
        JSONObject jsonObject = JsonDataFormate.piecing(userList,pageInfo);
        return jsonObject;
    }
}
